package org.jeecgframework.web.service.impl;

import org.jeecgframework.core.common.service.impl.CommonServiceImpl;
import org.jeecgframework.web.system.pojo.base.CbsServiceEntity;
import org.jeecgframework.web.system.pojo.base.CbsServiceLogEntity;
import org.jeecgframework.web.service.CbsServiceLogServiceI;
import org.jeecgframework.web.system.pojo.base.ZyFileLogEntity;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Service("cbsServiceLogService")
@Transactional
public class CbsServiceLogServiceImpl extends CommonServiceImpl implements CbsServiceLogServiceI {


    public <T> void delete(T entity) {
        super.delete(entity);
        //执行删除操作配置的sql增强
        this.doDelSql((CbsServiceLogEntity) entity);
    }

    public <T> Serializable save(T entity) {
        Serializable t = super.save(entity);
        //执行新增操作配置的sql增强
        this.doAddSql((CbsServiceLogEntity) entity);
        return t;
    }

    public <T> void saveOrUpdate(T entity) {
        super.saveOrUpdate(entity);
        //执行更新操作配置的sql增强
        this.doUpdateSql((CbsServiceLogEntity) entity);
    }

    /**
     * 默认按钮-sql增强-新增操作
     *
     * @param id
     * @return
     */
    public boolean doAddSql(CbsServiceLogEntity t) {
        return true;
    }

    /**
     * 默认按钮-sql增强-更新操作
     *
     * @param id
     * @return
     */
    public boolean doUpdateSql(CbsServiceLogEntity t) {
        return true;
    }

    /**
     * 默认按钮-sql增强-删除操作
     *
     * @param id
     * @return
     */
    public boolean doDelSql(CbsServiceLogEntity t) {
        return true;
    }

    @Override
    public List<Map<String, Object>> getServiceLog(String ServiceId) {
        StringBuffer sf = new StringBuffer();
        sf.append("SELECT\n");
        sf.append("\tlog.create_name AS createName,\n");
        sf.append("\tIFNULL(\n");
        sf.append("\t\tDATE_FORMAT(\n");
        sf.append("\t\t\tlog.create_date,\n");
        sf.append("\t\t\t'%Y-%m-%d  %H:%i:%s'\n");
        sf.append("\t\t),\n");
        sf.append("\t\t'无'\n");
        sf.append("\t) AS createDate,\n");
        sf.append("\tlog.description AS description\n");
        sf.append("FROM\n");
        sf.append("\tCBS_SERVICE_LOG log\n");
        sf.append("WHERE\n");
        sf.append("\tlog.log_type = 2 and log.cbs_service_id = '").append(ServiceId).append("' ORDER BY log.create_date DESC");
        return this.findForJdbc(sf.toString());
    }

    @Override
    public List<Map<String, Object>> getServiceLogOfTp(String ServiceId) {
        StringBuffer sf = new StringBuffer();
        sf.append("SELECT\n");
        sf.append("\tlog.create_name AS createName,\n");
        sf.append("\tIFNULL(\n");
        sf.append("\t\tDATE_FORMAT(\n");
        sf.append("\t\t\tlog.create_date,\n");
        sf.append("\t\t\t'%Y-%m-%d  %H:%i:%s'\n");
        sf.append("\t\t),\n");
        sf.append("\t\t'无'\n");
        sf.append("\t) AS createDate,\n");
        sf.append("\tlog.description AS description\n");
        sf.append("FROM\n");
        sf.append("\tCBS_SERVICE_LOG log\n");
        sf.append("WHERE\n");
        sf.append("\tlog.log_type = 2 and (log.type != 1 or log.type is NULL) and log.cbs_service_id = '").append(ServiceId).append("' ORDER BY log.create_date DESC");
        return this.findForJdbc(sf.toString());
    }

    @Override
    public List<Map<String, Object>> getAllFileByCbsId(String ServiceId) {
        StringBuffer sf = new StringBuffer();
        sf.append("SELECT\n" +
                "\tf.id AS id,\n" +
                "\tf.FILE_PATH AS path,\n" +
                "\tf.business_child_type AS type,\n" +
                "\tf.OLD_FILE_NAME AS NAME,\n" +
                "\tf.create_date AS date,\n" +
                "\tf.create_name AS createName\n" +
                "FROM\n" +
                "\tzy_file_log  f\n" +
                "WHERE\n" +
                "  f.file_type is NULL and f.business_key = '").append(ServiceId).append("'");
        return this.findForJdbc(sf.toString());
    }

    @Override
    public boolean addServiceLog(CbsServiceEntity cbsServiceId, Integer logType, String description, ZyFileLogEntity zyFileLogId) {

        CbsServiceLogEntity cbsServiceLogEntity = new CbsServiceLogEntity();
        cbsServiceLogEntity.setLogType(logType);
        cbsServiceLogEntity.setCbsServiceId(cbsServiceId);
        cbsServiceLogEntity.setDescription(description);
        cbsServiceLogEntity.setZyFileLogId(zyFileLogId);
        this.save(cbsServiceLogEntity);
        return false;
    }


    public void cbsServiceLog (String cbsServiceId){
        String sql ="UPDATE CBS_SERVICE_LOG SET type = 1 WHERE cbs_service_id = '"+cbsServiceId+"' AND id NOT IN ( SELECT id FROM ( SELECT id FROM CBS_SERVICE_LOG WHERE cbs_service_id = '"+cbsServiceId+"' ORDER BY create_date ASC LIMIT 0, 1 ) AS tp ) ORDER BY create_date DESC";
        this.updateBySqlString(sql);
    }

    /**
     * 替换sql中的变量
     *
     * @param sql
     * @return
     */
    public String replaceVal(String sql, CbsServiceLogEntity t) {
        sql = sql.replace("#{id}", String.valueOf(t.getId()));
        sql = sql.replace("#{description}", String.valueOf(t.getDescription()));
        sql = sql.replace("#{create_name}", String.valueOf(t.getCreateName()));
        sql = sql.replace("#{create_by}", String.valueOf(t.getCreateBy()));
        sql = sql.replace("#{create_date}", String.valueOf(t.getCreateDate()));
        sql = sql.replace("#{update_name}", String.valueOf(t.getUpdateName()));
        sql = sql.replace("#{update_by}", String.valueOf(t.getUpdateBy()));
        sql = sql.replace("#{update_date}", String.valueOf(t.getUpdateDate()));
        sql = sql.replace("#{UUID}", UUID.randomUUID().toString());
        return sql;
    }
}